package com.zzh.service.impl.authentication;
/*
 * @Auther : wenrouyue
 * @Date : 2022/5/30-15:37
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzh.entity.LoginUser;
import com.zzh.entity.User;

import com.zzh.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author wenrouyue
 */
@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private UserMapper userMapper;


    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getEmail,s);
        User user = userMapper.selectOne(wrapper);
        if (Objects.isNull(user)) {
            log.info("用戶:{},未注册",s);
            throw new RuntimeException("用户名或密码错误!");
        }
        List<String> list = userMapper.selectComponentByUserId(user.getId());
        //数据封装成UserDetails返回
        return new LoginUser(user,list);
    }
}
